package com.zhihuishu.micro.course.openapi.course.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.common.utils.Assert;
import com.zhihuishu.micro.course.model.LessonTestQuestion;
import com.zhihuishu.micro.course.openapi.course.LessonTestQuestionOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.LessonTestQuestionOpenDto;
import com.zhihuishu.micro.course.openapi.course.util.DataFormatConversionTotal;
import com.zhihuishu.micro.course.service.LessonTestQuestionService;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;
@Service("lessonTestQuestionOpenService")
public class LessonTestQuestionOpenServiceImpl implements LessonTestQuestionOpenService{

	@Resource
	private LessonTestQuestionService lessonTestQuestionService;
	
	private static final String moduleNameCN = "小节打点";
	/**
	 * 根据小节id和节id查找弹题关联对象
	 * @param lessonId 	节id 必选
	 * @param smallLessonId 小节id 可选
	 * @return
	 * @throws RemoteException 
	 * */
	public RemoteResult<List<LessonTestQuestionOpenDto>> getLessonTestQuestionListInfo(LessonTestQuestionOpenDto lessonTestQuestionOpenDtotemp) throws RemoteException{
		Assert.notNull(lessonTestQuestionOpenDtotemp.getLessonId(), "LessonId must be is not null");
		
		RemoteResult<List<LessonTestQuestionOpenDto>> result=new RemoteResult<List<LessonTestQuestionOpenDto>>();
		List<LessonTestQuestionOpenDto> lessonTestQuestionOpenDtoList=new ArrayList<LessonTestQuestionOpenDto>();
		if(lessonTestQuestionOpenDtotemp!=null){
			try {
				LessonTestQuestion lessonTestQuestiontemp=BeanHelper.transTo(lessonTestQuestionOpenDtotemp, LessonTestQuestion.class);
				List<LessonTestQuestion> lessonTestQuestionList=lessonTestQuestionService.getLessonTestQuestionListInfo(lessonTestQuestiontemp);
				
				if (!CollectionUtils.isEmpty(lessonTestQuestionList)){
					//把LessonTestQuestionList转换成lessonTestQuestionOpenDtoList
					for(LessonTestQuestion lessonTestQuestion : lessonTestQuestionList){
						if(lessonTestQuestion !=null){
							LessonTestQuestionOpenDto lessonTestQuestionOpenDto = BeanHelper.transTo(lessonTestQuestion,LessonTestQuestionOpenDto.class);
							//将timer转换成Long
							lessonTestQuestionOpenDto.setTime(DataFormatConversionTotal.time_stringToLong(lessonTestQuestion.getTimer(), 1));
							lessonTestQuestionOpenDtoList.add(lessonTestQuestionOpenDto);
						}		
					}					
				}	
				result.setSuccess(true);
				//排序
				lessonTestQuestionOpenDtoList=this.getSortLessonTestQuestionListInfo(lessonTestQuestionOpenDtoList);
				result.setResult(lessonTestQuestionOpenDtoList);
			} catch (Exception e) {
		         throw new RemoteException(e).setMessage("根据节id 小节id查询-" + moduleNameCN + "-报错!");
			}
		}

		return result;
	}
	
	
	//弹题时间点排序 （按照时间点自小到大排序）
	public List<LessonTestQuestionOpenDto> getSortLessonTestQuestionListInfo(List<LessonTestQuestionOpenDto> list) {

		//按time （秒） 排序
		if(!CollectionUtils.isEmpty(list)){
			
			Collections.sort(list, new Comparator<LessonTestQuestionOpenDto>() {  
				  
	            @SuppressWarnings("unused")
				@Override  
	            public int compare(LessonTestQuestionOpenDto o1, LessonTestQuestionOpenDto o2) { 
	            	
	            	if(o1 !=null && o2 !=null){	 
	            		
	            		Integer OrderNumber1=o1.getTime().intValue();
		            	Integer OrderNumber2=o2.getTime().intValue();
		            	
	            		if(OrderNumber1 !=null && OrderNumber2 !=null){
	            			
	            			int i = OrderNumber1 - OrderNumber2;  
			                if(i == 0){  
			                    return o1.getLessonTestQuestionId() - o2.getLessonTestQuestionId();  
			                }  
			                return i; 
			                
	            		}else if(OrderNumber1 !=null && OrderNumber2 ==null){
		            		return 1;
		            	}else if(OrderNumber1 ==null && OrderNumber2 !=null){
		            		return -1;
		            	}
	            		
	            	}
	            	
	            	return o1.getLessonTestQuestionId() - o2.getLessonTestQuestionId();
	            }  
	        });
		}
				
		return list;
	}
	
	 /**
	 * 加载数据
	 * 根据id获取视频弹题关联对象
	 * @param lessonTestQuestionId 
	 * @return
	 * @throws RemoteException 
	 * */
	public RemoteResult<LessonTestQuestionOpenDto> getLessonTestQuestionInfo(Integer lessonTestQuestionId) throws RemoteException{
		Assert.notNull(lessonTestQuestionId, "lessonTestQuestionId must be is not null");
		RemoteResult<LessonTestQuestionOpenDto> result=new RemoteResult<LessonTestQuestionOpenDto>();
		LessonTestQuestionOpenDto lessonTestQuestionOpenDto=new LessonTestQuestionOpenDto();
		
		try {
			LessonTestQuestion lessonTestQuestion=lessonTestQuestionService.getLessonTestQuestionInfo(lessonTestQuestionId);
			
			//把LessonTestQuestion转换成lessonTestQuestionOpenDto
			if(lessonTestQuestion !=null){
				lessonTestQuestionOpenDto=BeanHelper.transTo(lessonTestQuestion, LessonTestQuestionOpenDto.class);
				//将timer转换成Long
				lessonTestQuestionOpenDto.setTime(DataFormatConversionTotal.time_stringToLong(lessonTestQuestion.getTimer(), 1));
			}
			result.setSuccess(true);
			result.setResult(lessonTestQuestionOpenDto);
		} catch (Exception e) {
			 throw new RemoteException(e).setMessage("根据id 查询-" + moduleNameCN + "-报错!");
		}

		return result;
	}
	
	/**
	 * 保存弹题关联对象
	 * @param timer	小节视频打点时间
	 * @param lessonId 节id
	 * @param testQuestionId 试题id
	 * @param smallLessonId  小节id
	 * @param isDelete	是否删除  0.否1.是  默认保存0	
	 * @return
	 * @throws RemoteException 
	 * */
	public RemoteResult<LessonTestQuestionOpenDto> save(LessonTestQuestionOpenDto lessonTestQuestionOpenDto) throws RemoteException{
		Assert.notNull(lessonTestQuestionOpenDto.getLessonId(), "lessonId must be is not null");
		RemoteResult<LessonTestQuestionOpenDto> result=new RemoteResult<LessonTestQuestionOpenDto>();
		try {
			//对象转换
			LessonTestQuestion lessonTestQuestion=BeanHelper.transTo(lessonTestQuestionOpenDto, LessonTestQuestion.class);
			lessonTestQuestion=lessonTestQuestionService.save(lessonTestQuestion);
			
			if(lessonTestQuestion !=null && lessonTestQuestion.getLessonTestQuestionId()!=null){
				lessonTestQuestionOpenDto=BeanHelper.transTo(lessonTestQuestion, LessonTestQuestionOpenDto.class);		
			}
			result.setResult(lessonTestQuestionOpenDto);
			result.setSuccess(true);
		} catch (Exception e) {
			 throw new RemoteException(e).setMessage("保存-" + moduleNameCN + "-报错!");
		}

		return result;
	}
	
	/**
	 * 删除弹题关联对象
	 * @param id 弹题关联id
	 * @return
	 * @throws RemoteException 
	 * */
	public RemoteResult<Integer> remove(Integer id) throws RemoteException{
		Assert.notNull(id, "id must be is not null");;
		
		RemoteResult<Integer> result=new RemoteResult<Integer>();
		Integer temp=-1;	
		try {
			temp=lessonTestQuestionService.remove(id);
			result.setSuccess(true);
			result.setResult(temp);

		} catch (Exception e) {
			 throw new RemoteException(e).setMessage("删除-" + moduleNameCN + "-报错!");
		}

		return result;
	}
	
	/**
	 * 修改弹题关联对象
	 * @param id 弹题关联id
	 * @param timer	小节视频打点时间
	 * @param lessonId 节id
	 * @param testQuestionId 试题id
	 * @param smallLessonId  小节id
	 * @param isDelete	是否删除  0.否1.是  默认保存0	
	 * @return
	 * @throws RemoteException 
	 * */
	public RemoteResult<LessonTestQuestionOpenDto> update(LessonTestQuestionOpenDto lessonTestQuestionOpenDto) throws RemoteException{
		Assert.notNull(lessonTestQuestionOpenDto.getTimer(), "Timer must be is not null");
		Assert.notNull(lessonTestQuestionOpenDto.getLessonTestQuestionId(), "id must be is not null");
		RemoteResult<LessonTestQuestionOpenDto> result=new RemoteResult<LessonTestQuestionOpenDto>();	
		try {
			//对象转换
			LessonTestQuestion lessonTestQuestion=BeanHelper.transTo(lessonTestQuestionOpenDto, LessonTestQuestion.class);
			lessonTestQuestion=lessonTestQuestionService.update(lessonTestQuestion);
			if(lessonTestQuestion !=null ){
				lessonTestQuestionOpenDto=BeanHelper.transTo(lessonTestQuestion, LessonTestQuestionOpenDto.class);
			}
			result.setSuccess(true);
			result.setResult(lessonTestQuestionOpenDto);
			
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("删除-" + moduleNameCN + "-报错!");
		}

		return result;
	}
}
